package com.itfenghuang.day12;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Objects;

public class A06_历史遗留问题 {

//    5.分析以下需求，并用代码实现：
//            (1)定义一个Person类型(属性私有、空参有参构造、set、get方法、toString方法、equals方法、hashCode方法)，
//    包含三个属性：姓名(String name)、年龄(int age)
//	(2)List集合中存储多个Person对象,获取年龄最大的Person对象并打印信息
//            (3)思路
//    a.定义一个Person类型的变量, 先记住第一个元素
//    b.循环遍历集合
//    c.用每一个元素和变量比较年龄, 如果集合中的元素比变量记住的年龄大
//    d.用变量记住这个年龄较大的元素
//    e.返回变量记住的元素

//    6.分析以下需求，并用代码实现：
//            (1)定义一个学生类Student，属性：姓名(String name)、班级班号(String no)、成绩(double score)
//	(2)将若干Student对象存入List集合，并统计每个班级的总分和平均分
//    思路：
//    a.采用面向对象的思想
//    b.不推荐使用Map<String,List<Student>> 操作不方便
//    c.推荐使用Map<String，ClassRoom>

    public static void main(String[] args) {

//        method();
        ArrayList<Studnet> list = new ArrayList<>();
        list.add(new Studnet("张三","1班",60.11));
        list.add(new Studnet("张一","2班",78.11));
        list.add(new Studnet("张二","3班",69.11));
        list.add(new Studnet("张四","2班",52.11));
        HashMap<String, ClassRoom> map = new HashMap<>();
        for (Studnet stu : list) {
            //判断集合中有没有该班级
            if (map.containsKey(stu.getNo())){
                    //已经存在这个班级 通过班级号获取值
                ClassRoom classRoom = map.get(stu.getNo());
                // 修改 classroom中的人数和分数
                //获取班级目前的总分
                double v=classRoom.getTotal()+ stu.getScore();
                classRoom.setTotal(v);
                int i=classRoom.getNumber()+ 1;
                classRoom.setNumber(i);
                //将修改后的值添加到集合中
                ;map.put(stu.getNo(),classRoom);
            }else{
                map.put(stu.getNo(),new ClassRoom(stu.getNo(),stu.getScore(),0d,1));
            }
        }
        System.out.println(map);

    }

    private static void method() {
        LinkedList<Person> list = new LinkedList<>();
        list.add(new Person("tom",19));
        list.add(new Person("jack",22));
        list.add(new Person("too",59));
        list.add(new Person("bbb",29));
        Person p = list.get(0);
        for (int i = 0; i < list.size(); i++) {
            Person person = list.get(i);
            if (p.getAge()<person.getAge()){
                p=person;
            }
        }
        System.out.println(p);
    }
}
class Person{
    private String name;
    private  int age;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return age == person.age &&
                Objects.equals(name, person.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Person() {
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
}
class Studnet{
    private String name;
    private String no;
    private Double score;

    @Override
    public String toString() {
        return "Studnet{" +
                "name='" + name + '\'' +
                ", no='" + no + '\'' +
                ", score=" + score +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNo() {
        return no;
    }

    public void setNo(String no) {
        this.no = no;
    }

    public Double getScore() {
        return score;
    }

    public void setScore(Double score) {
        this.score = score;
    }

    public Studnet() {
    }

    public Studnet(String name, String no, Double score) {
        this.name = name;
        this.no = no;
        this.score = score;
    }
}
class ClassRoom{
    private String no;
    private Double total;
    private  Double avg;
    private  int   number;

    public ClassRoom() {
    }

    public ClassRoom(String no, Double total, Double avg, int number) {
        this.no = no;
        this.total = total;
        this.avg = avg;
        this.number = number;
    }

    @Override
    public String toString() {
        return "ClassRoom{" +
                "no='" + no + '\'' +
                ", total=" + total +
                ", avg=" + avg +
                ", number=" + number +
                '}';
    }

    public String getNo() {
        return no;
    }

    public void setNo(String no) {
        this.no = no;
    }

    public Double getTotal() {
        return total;
    }

    public void setTotal(Double total) {
        this.total = total;
    }

    public Double getAvg() {
        return avg;
    }

    public void setAvg(Double avg) {
        this.avg = avg;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }
}